(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.company.deleteRejectController', deleteRejectController);
	deleteRejectController.$inject = [ '$scope', 'Constants', 'modalService', 'layoutConfig', 'validatorConfig', 'gridService', 'authService', 'companyService' ];
	function deleteRejectController($scope, Constants, modalService, layoutConfig, validatorConfig, gridService, authService, companyService) {
		var ctrl = this, companyInfo = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.company = {};

		ctrl.init = function() {
			var params = {
				taskId : companyInfo.taskId
			};
			$scope.setPageDescription(function() {
				return ctrl.company ? ctrl.company.companyCode : null;
			});
			// init services
			companyService.initService($scope, ctrl);
			// init contacts
			companyService.initContact($scope, ctrl);	
			// init subcompany
			companyService.initSubCompany($scope, ctrl);
			// init notify tab
			companyService.initNotify($scope, ctrl);
			// init company report
			companyService.initReport($scope, ctrl);
			// init company address
			companyService.initAddress($scope, ctrl);
			// init company address
			companyService.initDynaParm($scope, ctrl);
			companyService.initEdiProfileGrid($scope, ctrl);
			
			ctrl.contactInnerGridOptions.enableRowHeaderSelection = false;
			ctrl.serviceInnerGridOptions.enableRowHeaderSelection = false;
			ctrl.subCompanyInnerGridOptions.enableRowHeaderSelection = false;
			ctrl.notifyInnerGridOptions.enableRowHeaderSelection = false;
			ctrl.reportInnerGridOptions.enableRowHeaderSelection = false;
			ctrl.addressInnerGridOptions.enableRowHeaderSelection = false;
			ctrl.selectedPropertyInnerGridOptions.enableRowHeaderSelection = false;
			ctrl.ediFileDeliInnerGridOptions.enableRowHeaderSelection = false;
			
			authService.deleteReject(params).then(function(response) {
				companyService.initGeneral($scope, ctrl, response.data.model).then(function(){
					ctrl.company = response.data.model;
					ctrl.company.taskId = response.data.task.taskId || companyInfo.taskId;
					//
					ctrl.company.companyRpts = ctrl.company.companyRpts || [];
					// init company ediprofile
					companyService.initEdiProfile($scope, ctrl);
					//init company classificationCode
					if (ctrl.classifiOptions && ctrl.classifiOptions.data && ctrl.classifiOptions.data[0]) {
						ctrl.company.classificationCode = ctrl.company.classificationCode || ctrl.classifiOptions.data[0].value;
					}
					angular.forEach(ctrl.company.companyRpts, function(companyRpt, key) {
						if (!ctrl.company['companyRpts' + companyRpt.report.serviceCode]) {								
							ctrl.company['companyRpts' + companyRpt.report.serviceCode] = [];
						}
						ctrl.company['companyRpts' + companyRpt.report.serviceCode].push(companyRpt);
					});
					ctrl.resetDynaProperties();
				});
			});
		};
		
		ctrl.deleteRejectSubmit = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.delete', args : [ctrl.company.companyNo]} ],
				'btnCallback' : {
					'ok' : function(response) {
						authService.deleteRejectSubmit(ctrl.company).then(deleteSuccessful, deleteFailed);
					}
				}
			});
		};
		
		ctrl.deleteRejectCancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.company.companyNo]} ],
				'btnCallback' : {
					'ok' : function(response) {
						authService.deleteRejectCancel(ctrl.company).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};
		
		function deleteSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.delete' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.company.list',
					'state' : 'cda.company.list',
					'params' : {}
				}]
			});
		}

		function deleteFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.company.list',
					'state' : 'cda.company.list',
					'params' : {}
				}]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
	}
})(angular);